package vns.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;

public class RoleDetailUI extends Composite{

	
	private List<String> listStringColumn = new ArrayList<String>();
	private List<String> listStringObjectType = new ArrayList<String>();
	private List<String> listStringProperty = new ArrayList<String>();
	private static int rowSelectedID = -1;
	private static int columnSelectedID = -1;
	private static int numofrow = 10;
	
	private static ObjectTypeDetailUiBinder uiBinder = GWT
			.create(ObjectTypeDetailUiBinder.class);
	@UiField FlexTable flexTableObjectTypeProperty;
	@UiField Button btSelectAll;
	@UiField Button btDeselectAll;
	@UiField Label labelRoleName;

	interface ObjectTypeDetailUiBinder extends
			UiBinder<Widget, RoleDetailUI> {
	}

	public RoleDetailUI() {
		initWidget(uiBinder.createAndBindUi(this));
		initFlexTableObjectTypeDetail();
	}
	
	public int getClickedRow(ClickEvent event) {
		int selectedRow = -1;
		HTMLTable.Cell cell = flexTableObjectTypeProperty
				.getCellForEvent(event);

		if (cell != null) {
			// Suppress clicks if not on the property button
			if (cell.getCellIndex() > 0) {
				selectedRow = cell.getRowIndex();
				rowSelectedID = selectedRow;
				columnSelectedID = cell.getCellIndex();
			}
		}
		
		
		return selectedRow;
	}
	
	public Widget asWidget(){
		return this;
	}
	
	/*
	 * Private
	 */
	
	private void initFlexTableObjectTypeDetail(){
		flexTableObjectTypeProperty.clear();
		flexTableObjectTypeProperty.setBorderWidth(1);
		flexTableObjectTypeProperty.setCellPadding(2);
		flexTableObjectTypeProperty.setCellSpacing(0);

		/*
		 * Tên các cột
		 */
		listStringColumn.add("Danh sách");
		listStringColumn.add("Dữ liệu");
		listStringColumn.add("Quyền xem");
		listStringColumn.add("Quyền xóa");
		listStringColumn.add("Quyền sửa");

		int i = 0;
		for (final String columnName : listStringColumn) {
			flexTableObjectTypeProperty.setHTML(0, i, "<center><b>"
					+ columnName + "</b></center>");
			i++;
		}
		
		int row = flexTableObjectTypeProperty.getRowCount();
		for (i=0; i<numofrow; i++){
			if(i==0 || i==6){
				flexTableObjectTypeProperty.setText(row + i, 0,
						"Danh sách khách hàng");
				for (int j = 2; j < 5; j++) {
					CheckBox cb = new CheckBox();
					cb.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {
							getClickedRow(event);
							ObjectTypeChecked();
						}
					});
					flexTableObjectTypeProperty.setWidget(row + i, j, cb);
				}
			}
			else {
				flexTableObjectTypeProperty.setText(row + i, 1, "Tên thuộc tính");
				flexTableObjectTypeProperty.setWidget(row + i, 2, new CheckBox());
				flexTableObjectTypeProperty.setWidget(row + i, 3, new CheckBox());
				flexTableObjectTypeProperty.setWidget(row + i, 4, new CheckBox());
			}
		}
		
		this.flexTableObjectTypeProperty.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getClickedRow(event);
			}
		});
	}
	
	public void ObjectTypeChecked(){
		int rowStart = rowSelectedID + 1;
		int rowEnd = rowSelectedID + 1;
		for(int i = rowStart; i < numofrow; i++){
			if(flexTableObjectTypeProperty.getText(i, 0) != ""){
				rowEnd = i - 1;
				break;
			}
		}
		CheckBox cb;

		cb = (CheckBox) flexTableObjectTypeProperty.getWidget(rowStart, columnSelectedID);
		for(int i=rowStart; i<=rowEnd; i++){
			cb = (CheckBox) flexTableObjectTypeProperty.getWidget(i, columnSelectedID);
			cb.setValue(!cb.getValue());
		}
	}
	
	@UiHandler("btUpdate")
	void onBtUpdateClick(ClickEvent event) {
	}
	@UiHandler("btSelectAll")
	void onBtSelectAllClick(ClickEvent event) {
		CheckBox ck;
		for(int i = 1; i<=numofrow; i++){
			ck = (CheckBox) this.flexTableObjectTypeProperty.getWidget(i, 2);
			ck.setValue(true);
			ck = (CheckBox) this.flexTableObjectTypeProperty.getWidget(i, 3);
			ck.setValue(true);
			ck = (CheckBox) this.flexTableObjectTypeProperty.getWidget(i, 4);
			ck.setValue(true);
		}
	}
	@UiHandler("btDeselectAll")
	void onBtDeselectAllClick(ClickEvent event) {
		CheckBox ck;
		for(int i = 1; i<=numofrow; i++){
			ck = (CheckBox) this.flexTableObjectTypeProperty.getWidget(i, 2);
			ck.setValue(false);
			ck = (CheckBox) this.flexTableObjectTypeProperty.getWidget(i, 3);
			ck.setValue(false);
			ck = (CheckBox) this.flexTableObjectTypeProperty.getWidget(i, 4);
			ck.setValue(false);
		}
	}
}
